import 'package:dio/dio.dart';
import 'package:wanandroid_flutter/api/dio_manager.dart';
import 'package:wanandroid_flutter/api/api.dart';

import 'package:wanandroid_flutter/model/banner_model.dart';
import 'package:wanandroid_flutter/model/article_model.dart';
import 'package:wanandroid_flutter/model/hotword_model.dart';
import 'package:wanandroid_flutter/model/navi_model.dart';
import 'package:wanandroid_flutter/model/project_list_model.dart';
import 'package:wanandroid_flutter/model/project_tree_model.dart';
import 'package:wanandroid_flutter/model/system_tree_content_model.dart';
import 'package:wanandroid_flutter/model/system_tree_model.dart';
import 'package:wanandroid_flutter/model/wxarticle_content_model.dart';
import 'package:wanandroid_flutter/model/wxarticle_title_model.dart';

class CommonService {
  void getBanner(Function callback) async {
    DioManager.singleton.dio
        .get(Api.homeCarousel, options: _getOptions())
        .then((response) {
      callback(BannerModel(response.data));
    });
  }

  void getArticleList(Function callback, int _page) async {
    DioManager.singleton.dio
        .get(Api.homeArticleList + "$_page/json", options: _getOptions())
        .then((response) {
      callback(ArticleModel(response.data));
    });
  }

  /// 获取知识体系列表
  void getSystemTree(Function callback) async {
    DioManager.singleton.dio
        .get(Api.systemTree, options: _getOptions())
        .then((response) {
      callback(SystemTreeModel(response.data));
    });
  }

  /// 获取知识体系列表详情
  void getSystemTreeContent(Function callback, int _page, int _id) async {
    DioManager.singleton.dio
        .get(Api.systemTreeContent + "$_page/json?cid=$_id",
            options: _getOptions())
        .then((response) {
      callback(SystemTreeContentModel(response.data));
    });
  }

  /// 获取公众号名称
  void getWxList(Function callback) async {
    DioManager.singleton.dio
        .get(Api.wxList, options: _getOptions())
        .then((response) {
      callback(WxArticleTitleModel(response.data));
    });
  }

  /// 获取公众号文章
  void getWxArticleList(Function callback, int _id, int _page) async {
    DioManager.singleton.dio
        .get(Api.wxArticleList + "$_id/$_page/json", options: _getOptions())
        .then((response) {
      callback(WxArticleContentModel(response.data));
    });
  }

  /// 获取导航列表数据
  void getNaviList(Function callback) async {
    DioManager.singleton.dio
        .get(Api.naviList, options: _getOptions())
        .then((response) {
      callback(NaviModel(response.data));
    });
  }

  /// 获取项目分类
  void getProjectTree(Function callback) async {
    DioManager.singleton.dio
        .get(Api.projectTree, options: _getOptions())
        .then((response) {
      callback(ProjectTreeModel(response.data));
    });
  }

  /// 获取项目列表
  void getProjectList(Function callback, int _page, int _id) async {
    DioManager.singleton.dio
        .get(Api.projectList + "$_page/json?cid=$_id", options: _getOptions())
        .then((response) {
      callback(ProjectTreeListModel(response.data));
    });
  }

  /// 获取搜索热词
  void getSearchHotWord(Function callback) async {
    DioManager.singleton.dio
        .get(Api.searchHotWord, options: _getOptions())
        .then((response) {
      callback(HotWordModel(response.data));
    });
  }

  /// 获取搜索结果
  void getSearchResult(Function callback, int _page, String _id) async {
    FormData formData = new FormData.from({
      "k": _id,
    });
    DioManager.singleton.dio
        .post(Api.searchResult + "$_page/json",
            data: formData, options: _getOptions())
        .then((response) {
      callback(ArticleModel(response.data));
    });
  }

  Options _getOptions() {
    return null;
  }
}
